//
//  RegisnVC.m
//  智慧小区
//
//  Created by 高永杨 on 2017/3/20.
//  Copyright © 2017年 zhongchuang. All rights reserved.
//

#import "RegisnVC.h"

@interface RegisnVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *sureBut;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *identifyingTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFiel;

@end

@implementation RegisnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    UIControl *control = [[UIControl alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [control addTarget:self action:@selector(clikResignKeyBord) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:control atIndex:0];
    self.navigationItem.title = @"注册";
    //监听当键盘将要出现时
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    // Do any additional setup after loading the view from its nib.
}
-(void)clikResignKeyBord
{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.y = 0;
    }];
    [self.view endEditing:YES];
}
//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    int height = keyboardRect.size.height;
    GYYLog(@"------%f",(self.sureBut.y+self.sureBut.height));
    if (height>GyyScreenH-(self.sureBut.y+self.sureBut.height)) {
        GYYLog(@"-----%f",GyyScreenH-(self.sureBut.y+self.sureBut.height));
        [UIView animateWithDuration:1 animations:^{
            self.view.y = -(height-(GyyScreenH-(self.sureBut.y+self.sureBut.height)))-10;
        }];
    }
}
- (IBAction)goToBack:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)identifyingCode:(UIButton *)sender {
    if (![self isMobileNumber:self.phoneTextField.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
            return;
    }
    NSMutableDictionary *dicTionary = [NSMutableDictionary dictionary];
    [dicTionary setValue:@"32" forKey:@"opt"];
    [dicTionary setValue:self.phoneTextField.text forKey:@"mobile"];
    [dicTionary setValue:@"1" forKey:@"flag"];
    [[AFHTTPSessionManager manager] POST:GolobaUrl parameters:dicTionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    if (mobileNum.length != 11)
    {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
     * 电信号段: 133,149,153,170,173,177,180,181,189
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,145,155,156,170,171,175,176,185,186
     */
    NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,149,153,170,173,177,180,181,189
     */
    NSString *CT = @"^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (IBAction)registBut:(UIButton *)sender {
    if ([self.phoneTextField.text isEqualToString:@""]||[self.passwordTextFiel.text isEqualToString:@""]||[self.identifyingTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"输入的内容不能为空"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.phoneTextField.text forKey:@"mobile"];
    [dict setValue:self.passwordTextFiel.text forKey:@"pwd"];
    [dict setValue:self.identifyingTextField.text forKey:@"code"];
    [dict setValue:[UIDevice currentDevice].identifierForVendor.UUIDString forKey:@"equipment"];
    [dict setValue:@"10" forKey:@"opt"];
    [[AFHTTPSessionManager manager] POST:GolobaUrl parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD showSuccessWithStatus:@"注册成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self clikResignKeyBord];
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
